package com.lolc.ihm.component;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;

import com.lolc.LoLAutoCounterManager;
import com.lolc.bean.Champion;
import com.lolc.ihm.Constants;
import com.lolc.ihm.MainWindow;

public class MessagesWindow extends JDialog {

	private boolean withProgressBar=false;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		
	public MessagesWindow() {
		try {
			jbInit();
		}
		catch(Exception e) {
			LoLAutoCounterManager.getLogger().error(e.getCause());
		}
	}
	
	public MessagesWindow(boolean withProgressBar, int max) {
		try {
			this.withProgressBar=withProgressBar;
			this.progressbar.setMaximum(max);
			jbInit();
		}
		catch(Exception e) {
			LoLAutoCounterManager.getLogger().error(e.getCause());
		}
	}
	
	public void initialise( List<Champion> champions){
		for(Champion champion : champions)
		{			
			listModel.addElement(champion.getName());
		}		
	}

	public void display(){		
		pack(); 
		setLocationRelativeTo(MainWindow.getInstance());		
		setVisible(true);
	}
	
	public void show(List<Champion> champions){		
		initialise(champions);
		pack(); 
		validate();
		setLocationRelativeTo(MainWindow.getInstance());		
		setVisible(true);
	}

	//Main Panel
	private JPanel jPanel = new JPanel();
	
	//Panel with the message and progress bar
	private JPanel jPanel3 = new JPanel();
	private JLabel message = new JLabel();
	private JProgressBar progressbar = new JProgressBar(0, 100);
	  
	//Panel with the list
	private JPanel jPanel1 = new JPanel();
	private JList<String> jList = new JList<String>();
	private DefaultListModel<String> listModel;
	
	//Panel with button
	private JPanel jPanel2 = new JPanel();
	private JButton jButton1 = new JButton();

	private void jbInit() throws Exception {
		
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setResizable(false);		
		
		jPanel.setBackground(Constants.BACKGROUND_COLOR);	
		jPanel.setLayout(new GridBagLayout());

		GridBagConstraints gbc = new GridBagConstraints();
		int y=0;
		
		gbc.gridx = 0;
		gbc.gridy = y;
		gbc.weighty = 0.5;
		
		/** 
		 * Progress bar panel
		 */
		if(this.withProgressBar)
		{
			jPanel3.setPreferredSize(new Dimension(Constants.RESULT_WINDOW_WIDTH, Constants.PROGRESS_BAR_PANEL_WIDTH));
			jPanel3.setLayout(new GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.fill = GridBagConstraints.BOTH;
			c.gridx = 0;
			c.gridy = 0;
			c.weighty = 0.5;
			c.anchor =  GridBagConstraints.FIRST_LINE_START;

			jPanel3.setBackground(Constants.BACKGROUND_COLOR);	
			message.setForeground(Color.WHITE);
			message.setText("Test");
			jPanel3.add(message,c);

			c = new GridBagConstraints();
			c.gridy = 1;
			c.ipadx=Constants.RESULT_WINDOW_WIDTH-2*Constants.RESULT_WINDOW_SPACE_AROUND_LIST;
			progressbar.setForeground(Constants.COLOR_DARK_BLUE);
			progressbar.setStringPainted(true); // display %
			jPanel3.add(progressbar,c);
			jPanel.add(jPanel3,  gbc);
			y++;
		}
		
		/**
		 * List with all messages
		 */
		jPanel1.setPreferredSize(new Dimension(Constants.RESULT_WINDOW_WIDTH, Constants.RESULT_WINDOW_HEIGHT));
		jPanel1.setLayout(null);
		jPanel1.setBackground(Constants.BACKGROUND_COLOR);		
		listModel = new DefaultListModel<String>();
		jList = new JList<String>(listModel);
		jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jList.setLayoutOrientation(JList.VERTICAL);
		jList.setBorder(new LineBorder(Constants.COLOR_GRAY_2, 2));
		
		JScrollPane listScroller = new JScrollPane(jList);
		listScroller.setBounds(new Rectangle(Constants.RESULT_WINDOW_SPACE_AROUND_LIST, Constants.RESULT_WINDOW_SPACE_AROUND_LIST, Constants.RESULT_WINDOW_WIDTH-2*Constants.RESULT_WINDOW_SPACE_AROUND_LIST,Constants.RESULT_WINDOW_HEIGHT-2*Constants.RESULT_WINDOW_SPACE_AROUND_LIST));
		jPanel1.add(listScroller, null);
		
		/**
		 * Close button
		 */
		//jPanel2.setLayout(new FlowLayout());
		jPanel2.setBackground(Constants.BACKGROUND_COLOR);
		jButton1.setText(Constants.RESULT_WINDOW_OK_BTN);
		jButton1.addActionListener(new ActionListener() {
	        public void actionPerformed(ActionEvent e){
	            Container frame = jButton1.getParent();
	            do 
	                frame = frame.getParent(); 
	            while (!(frame instanceof JDialog));                                      
	            ((JDialog) frame).dispose();;
	         
	        }
	    });
		jPanel2.add(jButton1, null);
					
		gbc.gridy = y;
		jPanel.add(jPanel1, gbc);	
		y++;
		
		gbc.gridy = y;
		jPanel.add(jPanel2,  gbc);
		
		this.getContentPane().add(jPanel);
	}

	/**
	 * Update the message on the top of the progress bar and the value of the progress bar
	 * and append it in the message list if displayInMsgList
	 * @param message : the message to be displayed
	 * @param value : the progress value
	 * @param displayInMsgList specify if the message has to be append in the message list.
	 */
	public void updateMessageAndProgressBar(String message, int value, boolean displayInMsgList)
	{
		this.message.setText(message);
		this.progressbar.setValue(value);
		if(displayInMsgList)
			appendMessageInList(message);
		
		refreshPanel();
	}
	
	/**
	 * Update the message on the top of the progress bar and the value of the progress bar
	 * and append it in the message list if displayInMsgList
	 * @param message : the message to be displayed
	 * @param value : the progress value
	 * @param displayInMsgList specify if the message has to be append in the message list.
	 */
	public void updateProgressBar(int value)
	{
		this.progressbar.setValue(value);
		refreshPanel();
	}
	
	/**
	 * Update the maximum value of the progress bar
	 * @param max
	 */
	public void updateProgressBarMaximum(int max)
	{
		this.progressbar.setMaximum(max);
		refreshPanel();
	}
	
	/**
	 * Display message in the message list and scroll to the last message
	 * @param message the message to be append
	 */
	
	public void appendMessageInList(String message)
	{
		listModel.addElement(message);
	
		//Scroll to last item
		int lastIndex = jList.getModel().getSize() - 1;
		if (lastIndex >= 0) {
			jList.ensureIndexIsVisible(lastIndex);
		}
		
		refreshPanel();
	}

	private void refreshPanel()
	{
		jPanel.revalidate();
		jPanel.paintImmediately(jPanel.getBounds());	
	}
}